package org.linlinjava.litemall.wx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallNotice;
import org.linlinjava.litemall.db.domain.LitemallNoticeWithBLOBs;
import org.linlinjava.litemall.db.service.LitemallNoticeService;
import org.linlinjava.litemall.wx.annotation.LogAnno;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxMsgService;
import org.linlinjava.litemall.wx.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * wx味精控制器
 *
 * @author yliyun
 * @description：TODO
 * @date 2022/09/14
 */
@Api(tags = "消息助手API")
@RestController
@RequestMapping("/wx/msg")
@RequiredArgsConstructor
public class WxMsgController extends ResponseUtil{


    private final LitemallNoticeService litemallMsgService;

    private final WxMsgService msgService;


    @ApiOperation("消息-「类型|存在消息」")
    @GetMapping("/msgType")
    @LogAnno
    public Response msgType(@ApiIgnore @LoginUser Integer userId){
        return msgService.msgType(userId);
    }

    @ApiOperation("消息-「1：系统消息；2订单消息；3推广消息；4其他消息」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "page"),
            @ApiImplicitParam(name = "limit",value = "limit"),
            @ApiImplicitParam(name = "type",value = "类型")
    })
    @GetMapping("/systemMsg")
    @LogAnno
    public Response systemMsg(@ApiIgnore @LoginUser Integer userId,
                              @RequestParam("type") String type,
                              @RequestParam(name = "page",defaultValue = "1")Integer page,
                              @RequestParam(name = "limit",defaultValue = "20") Integer limit){
        return msgService.systemMsg(userId,page,limit,type);
    }

    @ApiOperation("消息-「最近｜15条」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "page"),
            @ApiImplicitParam(name = "limit",value = "limit")
    })
    @GetMapping("/recentlyList")
    @LogAnno
    public Response recentlyList(@ApiIgnore @LoginUser Integer userId,
                                 @RequestParam(name = "page",defaultValue = "1")Integer page,
                                 @RequestParam(name = "limit",defaultValue = "15") Integer limit){
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return msgService.recentlyList(userId,page,limit);
    }

    @ApiOperation("消息-「列表」")
    @GetMapping("/list")
    @LogAnno
    public Object list(@LoginUser Integer userId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order){
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        List<LitemallNoticeWithBLOBs> noticeList = litemallMsgService.querySelective(userId, null, null, sort, order);
        Pageable pageable = PageRequest.of(page - 1,limit);
        return ResponseUtil.ok(PageUtil.getPageLumierePayMemberUserLogVo(pageable,noticeList));
    }


    @ApiOperation("消息-「标记已读」")
    @PostMapping("/mark/{id}")
    @LogAnno
    public Object markRead(@LoginUser Integer userId, @PathVariable Integer id){
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        LitemallNoticeWithBLOBs msg = new LitemallNoticeWithBLOBs();
        msg.setId(id);
        msg.setMarkRead(true);
        litemallMsgService.updateById(msg);
        return ResponseUtil.ok();
    }


    @ApiOperation("消息-「标记所有已读」")
    @PostMapping("/mark/all")
    @LogAnno
    public Object markAll(@LoginUser Integer userId){
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        LitemallNotice msg = new LitemallNotice();
        msg.setUserId(userId);
        msg.setMarkRead(true);
        return ResponseUtil.ok(litemallMsgService.updateMarkByUserId(msg));
    }


    @ApiOperation("消息-「删除」")
    @GetMapping("/delete")
    @LogAnno
    public Object delete(@LoginUser Integer userId, @RequestParam("id")  Integer id){
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        if(id==null){
            return ResponseUtil.fail("未得到删除的消息ID");
        }
        litemallMsgService.deleteById(id);
        return ResponseUtil.ok();
    }


    @ApiOperation("消息-「删除所有」")
    @DeleteMapping("/delete/all")
    @LogAnno
    public Object deleteAll(@LoginUser Integer userId){
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        LitemallNotice msg = new LitemallNotice();
        msg.setUserId(userId);
        msg.setMarkRead(true);
        return ResponseUtil.ok(litemallMsgService.deleteByUserId(userId));
    }

    /**
     * 删除所有
     *
     * @param userId 用户id
     * @return {@link Object}
     */
    @GetMapping("notMark")
    @LogAnno
    public Object notMark(@LoginUser Integer userId){
        Integer count = litemallMsgService.queryNoMarkByUserId(userId);
        return ResponseUtil.ok(count);
    }
}
